import 'dart:async';
import 'package:flutter/services.dart';

class SobotApi {
  String startZhichiMethod = 'ZCSobot_Plugin_Start_SDK';
  String openSobotHelpCenterMethod = 'ZCSobot_Plugin_Open_Help_Center';
  String openSobotHelpMallCenterMethod = 'ZCSobot_Plugin_Open_Help_Mall_Center';
  String closeSobotChatMethod = 'ZCSobot_Plugin_Close_Sobot_Chat';
  String getUnReadMessageMethod = 'ZCSobot_Plugin_Get_Unread_Message';
  String messageLinkClickMethod = 'ZCSobot_Plugin_Message_Link_Click';
  String sendOrderGoodsInfoMethod = 'ZCSobot_Plugin_Send_Order_Goods_Info';
  String sendProductInfoMethod = 'ZCSobot_Plugin_Send_Product_Info';
  String closeSobotChatPageMethod = 'ZCSobot_Plugin_Close_Sobot_Chat_Page';
  String outCurrentUserIsClosePushMethod ='ZCSobot_Plugin_Out_Current_User_Is_Close_Push';
  // 程序进入后台的时候 移除IM监听和关闭通道 （iOS 使用）
  String closeIMCommectionAndRemoveObserverMethod = 'ZCSobot_Plugin_Close_IM_Connection_And_Remove_Observer';
// 程序进入前台的时候，添加IM监听和检查IM通道 (iOS 使用)
  String checkIMConnectedAndAddObserverMethod = 'ZCSobot_Plugin_Check_IM_Connection_And_Add_Observer';
// 发送自定义卡片
  String sendCustomCardMethod = 'ZCSobot_Plugin_Send_Custom_Card';
  //交互的通道名称，flutter和native是通过这个标识符进行相互间的通信
  static const sobotMethodChannel = MethodChannel('flutter_sobot_client');

  //await会阻塞流程，等待紧跟着的的Future执行完毕之后，再执行下一条语句
  //异步执行调用原生方法，保持页面不卡住，因为调用原生的方法可能没实现会抛出异常，所以trycatch包住
  Future<dynamic> startZhichi(initParams) async {
    try {
      final result =
      sobotMethodChannel.invokeMethod(startZhichiMethod, initParams);
      return result;
    } on PlatformException catch (e) {
      //抛出异常
      print(e);
      return {};
    }
  }

  Future<dynamic> sendCustomCard(params) async {
    try {
      final result = sobotMethodChannel.invokeMethod(sendCustomCardMethod,params);
      return result;
    } on PlatformException catch (e) {
      print(e);
      //抛出异常
      return {};
    }
  }

  Future<dynamic> openSobotHelpCenter(initParams) async {
    try {
      final result = sobotMethodChannel.invokeMethod(
          openSobotHelpCenterMethod, initParams);
      return result;
    } on PlatformException catch (e) {
      //抛出异常
      print(e);
      return {};
    }
  }

  Future<dynamic> openSobotHelpMallCenter(initParams) async {
    try {
      final result = sobotMethodChannel.invokeMethod(
          openSobotHelpMallCenterMethod, initParams);
      return result;
    } on PlatformException catch (e) {
      //抛出异常
      print(e);
      return {};
    }
  }

  Future<dynamic> closeSobotChatPage(initParams) async {
    try {
      final result = sobotMethodChannel.invokeMethod(
          closeSobotChatPageMethod, initParams);
      return result;
    } on PlatformException catch (e) {
      //抛出异常
      print(e);
      return {};
    }
  }

  Future<dynamic> getUnReadMessage(partnerid) async {
    try {
      /*
          结果 type: 4 未读消息
          value: 未读消息数量
          desc: 文本获取未读消息数
          */
      final result =
      sobotMethodChannel.invokeMethod(getUnReadMessageMethod, partnerid);
      return result;
    } on PlatformException catch (e) {
      //抛出异常
      print(e);
      return {};
    }
  }

  Future<dynamic> closeSobotChat(params) async {
    try {
      final result = sobotMethodChannel.invokeMethod(closeSobotChatMethod,params);
      return result;
    } on PlatformException catch (e) {
      print(e);
      //抛出异常
      return {};
    }
  }

  Future<dynamic> sendOrderGoodsInfo(params) async {
    try {
      final result = sobotMethodChannel.invokeMethod(sendOrderGoodsInfoMethod,params);
      return result;
    } on PlatformException catch (e) {
      print(e);
      //抛出异常
      return {};
    }
  }

  Future<dynamic> sendProductInfo(params) async {
    try {
      final result = sobotMethodChannel.invokeMethod(sendProductInfoMethod,params);
      return result;
    } on PlatformException catch (e) {
      print(e);
      //抛出异常
      return {};
    }
  }

  Future<dynamic> messageLinkClic() async {
    try {
      final result = sobotMethodChannel.invokeMethod(messageLinkClickMethod);
      return result;
    } on PlatformException catch (e) {
      print(e);
      //抛出异常
      return {};
    }
  }

  Future<dynamic> outCurrentUserIsClosePush(params) async {
    try {
      final result = sobotMethodChannel.invokeMethod(outCurrentUserIsClosePushMethod,params);
      return result;
    } on PlatformException catch (e) {
      print(e);
      //抛出异常
      return {};
    }
  }

  Future<dynamic> closeIMCommectionAndRemoveObserver(params) async {
    try {
      final result = sobotMethodChannel.invokeMethod(closeIMCommectionAndRemoveObserverMethod,params);
      return result;
    } on PlatformException catch (e) {
      print(e);
      //抛出异常
      return {};
    }
  }

  Future<dynamic> checkIMConnectedAndAddObserver(params) async {
    try {
      final result = sobotMethodChannel.invokeMethod(checkIMConnectedAndAddObserverMethod,params);
      return result;
    } on PlatformException catch (e) {
      print(e);
      //抛出异常
      return {};
    }
  }



}


